package com.czl.base.data.bean.tengyun;

import android.os.Parcel;
import android.os.Parcelable;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.bean.tengyun.SortBean;

import java.util.ArrayList;
import java.util.List;

public class RecordRemandBean extends BaseNumBean implements Parcelable, MultiItemEntity {

    public static final int VIEW_TYPE_PARENT = 1;
    public static final int VIEW_TYPE_CHILDREN = 2;

    //待还数量
    private Integer needReturnNum;
    //记录单id
    private Integer recordId;
    //使用类型：1.借用，2.领用
    private Integer applyType;
    private Integer recordType;
    //归还数量
    private Integer returnNum;
    private String recordCode;
    //借用人或者领用人
    private String userName;

    private String userId;

    private List<SortBean> sortList;
    private List<ProductBean> productList;
    private List<RecordRemandBean> recordIdList;

    private int itemViewType;

    private SortBean mSortBean;

    private RecordRemandBean mParentBean;
    private String sortName;
    private String damageComment;
    private String sortModel;
    private String dateStr12;
    private Integer damageNum;
    private Integer lossesNum;
    private Integer lendNum;
    private Integer excNum;
    private Integer excLossesNum;
    private Integer excLocatId;
    private Integer recordNum;

    public RecordRemandBean() {
    }


    protected RecordRemandBean(Parcel in) {
        if (in.readByte() == 0) {
            needReturnNum = null;
        } else {
            needReturnNum = in.readInt();
        }
        if (in.readByte() == 0) {
            recordId = null;
        } else {
            recordId = in.readInt();
        }
        if (in.readByte() == 0) {
            applyType = null;
        } else {
            applyType = in.readInt();
        }
        if (in.readByte() == 0) {
            recordType = null;
        } else {
            recordType = in.readInt();
        }
        if (in.readByte() == 0) {
            returnNum = null;
        } else {
            returnNum = in.readInt();
        }
        recordCode = in.readString();
        userName = in.readString();
        userId = in.readString();
//        sortList = in.createTypedArrayList(SortBean.CREATOR);
        productList = in.createTypedArrayList(ProductBean.CREATOR);
        recordIdList = in.createTypedArrayList(RecordRemandBean.CREATOR);
        itemViewType = in.readInt();
        mSortBean = in.readParcelable(SortBean.class.getClassLoader());
        mParentBean = in.readParcelable(RecordRemandBean.class.getClassLoader());
        sortName = in.readString();
        damageComment = in.readString();
        sortModel = in.readString();
        dateStr12 = in.readString();
        if (in.readByte() == 0) {
            damageNum = null;
        } else {
            damageNum = in.readInt();
        }
        if (in.readByte() == 0) {
            lossesNum = null;
        } else {
            lossesNum = in.readInt();
        }
        if (in.readByte() == 0) {
            lendNum = null;
        } else {
            lendNum = in.readInt();
        }
        if (in.readByte() == 0) {
            excNum = null;
        } else {
            excNum = in.readInt();
        }
        if (in.readByte() == 0) {
            excLossesNum = null;
        } else {
            excLossesNum = in.readInt();
        }
        if (in.readByte() == 0) {
            excLocatId = null;
        } else {
            excLocatId = in.readInt();
        }
        if (in.readByte() == 0) {
            recordNum = null;
        } else {
            recordNum = in.readInt();
        }
    }

    public static final Creator<RecordRemandBean> CREATOR = new Creator<RecordRemandBean>() {
        @Override
        public RecordRemandBean createFromParcel(Parcel in) {
            return new RecordRemandBean(in);
        }

        @Override
        public RecordRemandBean[] newArray(int size) {
            return new RecordRemandBean[size];
        }
    };

    public List<ProductBean> getProductList() {
        return productList;
    }

    public int getProductListSize() {
        return productList == null ? 0 : productList.size();
    }

    public void setProductList(List<ProductBean> productList) {
        this.productList = productList;
    }

    public List<RecordRemandBean> getRecordIdList() {
        return recordIdList;
    }

    public void setRecordIdList(List<RecordRemandBean> recordIdList) {
        this.recordIdList = recordIdList;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Integer getRecordNum() {
        return recordNum;
    }

    public void setRecordNum(Integer recordNum) {
        this.recordNum = recordNum;
    }

    public Integer getDamageNum() {
        return damageNum;
    }

    public int getDamageNumInt() {
        return damageNum == null ? 0 : damageNum;
    }

    public int damageNum() {
        if (damageNum == null) {
            return 0;
        }
        if (lossesNum == null) {
            return damageNum;
        }
        return damageNum - lossesNum;
    }
    public void setDamageNum(Integer damageNum) {
        this.damageNum = damageNum;
    }

    public Integer getLossesNum() {
        return lossesNum;
    }

    public int getLossesNumInt() {
        return lossesNum == null ? 0 : lossesNum;
    }

    public void setLossesNum(Integer lossesNum) {
        this.lossesNum = lossesNum;
    }

    public String getDateStr12() {
        return dateStr12;
    }

    public String getDateStr12NotNull() {
        return dateStr12 == null ? "" : dateStr12;
    }

    public String getDateStr8NotNull() {
        if (dateStr12 == null || dateStr12.isEmpty()) {
            return "";
        }
        String[] s = dateStr12.split(" ");
        if (s != null && s.length >= 1) {
            return s[0];
        }
        return dateStr12;
    }

    public void setDateStr12(String dateStr12) {
        this.dateStr12 = dateStr12;
    }

    public String getSortName() {
        return sortName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public String getDamageComment() {
        return damageComment;
    }

    public String getDamageCommentNotNull() {
        return damageComment == null ? "" : damageComment;
    }

    public void setDamageComment(String damageComment) {
        this.damageComment = damageComment;
    }

    public String getSortModel() {
        return sortModel;
    }

    public String getSortModelNotNull() {
        return sortModel == null ? "" : sortModel;
    }

    public void setSortModel(String sortModel) {
        this.sortModel = sortModel;
    }

    public int getReturnNumInt() {
        return getReturnNum() == null ? 0 : getReturnNum();
    }

    public Integer getReturnNum() {
        return returnNum;
    }

    public void setReturnNum(Integer returnNum) {
        this.returnNum = returnNum;
    }

    public Integer getNeedReturnNum() {
        return needReturnNum;
    }

    public int getNeedReturnNumInt() {
        return needReturnNum == null ? 0 : needReturnNum;
    }

    public Integer getExcNum() {
        return excNum;
    }

    public int getExcNumInt() {
        return excNum == null ? 0 : excNum;
    }

    public void setExcNum(Integer excNum) {
        this.excNum = excNum;
    }

    public Integer getExcLossesNum() {
        return excLossesNum;
    }

    public int getExcLossesNumInt() {
        return excLossesNum == null ? 0 : excLossesNum;
    }

    public void setExcLossesNum(Integer excLossesNum) {
        this.excLossesNum = excLossesNum;
    }

    public Integer getExcLocatId() {
        return excLocatId;
    }

    public void setExcLocatId(Integer excLocatId) {
        this.excLocatId = excLocatId;
    }

    public void setNeedReturnNum(Integer needReturnNum) {
        this.needReturnNum = needReturnNum;
    }

    public Integer getRecordId() {
        return recordId;
    }

    public void setRecordId(Integer recordId) {
        this.recordId = recordId;
    }

    public Integer getApplyType() {
        return applyType;
    }

    public void setApplyType(Integer applyType) {
        this.applyType = applyType;
    }

    public String getRecordCode() {
        return recordCode;
    }

    public String getRecordCodeNotNull() {
        return recordCode == null ? "" : recordCode;
    }

    public void setRecordCode(String recordCode) {
        this.recordCode = recordCode;
    }

    public String getUserName() {
        return userName;
    }

    public String getUserNameNotNull() {
        return userName == null ? "" : userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public List<SortBean> getSortList() {
        return sortList;
    }

    public void setSortList(List<SortBean> sortList) {
        this.sortList = sortList;
    }

    public SortBean getSortBean() {
        return mSortBean;
    }

    public void setSortBean(SortBean sortBean) {
        mSortBean = sortBean;
    }

    public RecordRemandBean getParentBean() {
        return mParentBean;
    }

    public void setParentBean(RecordRemandBean parentBean) {
        mParentBean = parentBean;
    }

    public void setRecordType(Integer recordType) {
        this.recordType = recordType;
    }

    public Integer getLendNum() {
        return lendNum;
    }

    public int getLendNumInt() {
        return lendNum == null ? 0 : lendNum;
    }

    public void setLendNum(Integer lendNum) {
        this.lendNum = lendNum;
    }

    public void setItemViewType(int itemViewType) {
        this.itemViewType = itemViewType;
    }

    @Override
    public int getItemType() {
        return itemViewType;
    }

    public static List<RecordRemandBean> getDiffList(List<RecordRemandBean> oldList, List<RecordRemandBean> newData) {
        List<RecordRemandBean> beanList = new ArrayList<>(oldList);
        for (RecordRemandBean recordRemandBean : oldList) {
            if (recordRemandBean.getRecordCode() == null) {
                continue;
            }
            for (RecordRemandBean remandBean : newData) {
                if (recordRemandBean.getRecordCode().equals(remandBean.getRecordCode())) {
                    beanList.remove(recordRemandBean);
                    break;
                }
            }
        }
        return beanList;
    }

    public static int getTotalDamageNum(List<RecordRemandBean> list) {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        int totalNum = 0;
        for (RecordRemandBean recordRemandBean : list) {
            totalNum += recordRemandBean.getExcNumInt();
        }
        return totalNum;
    }

    public String getRemandCode() {
        if (applyType == null) {
            return "";
        }
        if (1 == applyType) {
            return "借用单：" + getRecordCode();
        } else {
            return "领用单：" + getRecordCode();
        }
    }

    public String getRecordTitle() {
        if (getRemandCode() == null || getRemandCode().isEmpty()) {
            if (recordNum == null) {
                return "";
            }
            return "单数：" + recordNum;
        }
        return getRemandCode();
    }

    public String getRecordType() {
        if (applyType != null) {
            if (1 == applyType) {
                return "借用单：";
            } else {
                return "领用单：";
            }
        }
        if (recordType != null) {
            if (1 == recordType) {
                return "借用单：";
            } else {
                return "领用单：";
            }
        }
        return "";
    }

    public String getRemandUser() {
        if (applyType != null) {
            if (1 == applyType) {
                return "借用人：" + getUserNameNotNull();
            } else {
                return "领用人：" + getUserNameNotNull();
            }
        }
        if (recordType != null) {
            if (1 == recordType) {
                return "借用人：" + getUserNameNotNull();
            } else {
                return "领用人：" + getUserNameNotNull();
            }
        }
        return "姓名：" + getUserNameNotNull();
    }

    public String getBorRecNum() {
        if (applyType != null) {
            if (1 == applyType) {
                return "已借：" + getUserNameNotNull();
            } else {
                return "已领：" + getUserNameNotNull();
            }
        }
        if (recordType != null) {
            if (1 == recordType) {
                return "已借：" + getUserNameNotNull();
            } else {
                return "已领：" + getUserNameNotNull();
            }
        }
        return "数量：" + getLendNumInt();
    }

    public boolean isRemandVisable() {
        if (returnNum == null) {
            return false;
        }
        return returnNum > 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (needReturnNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(needReturnNum);
        }
        if (recordId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(recordId);
        }
        if (applyType == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(applyType);
        }
        if (recordType == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(recordType);
        }
        if (returnNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(returnNum);
        }
        dest.writeString(recordCode);
        dest.writeString(userName);
        dest.writeString(userId);
//        dest.writeTypedList(sortList);
        dest.writeTypedList(productList);
        dest.writeTypedList(recordIdList);
        dest.writeInt(itemViewType);
        dest.writeParcelable(mSortBean, flags);
        dest.writeParcelable(mParentBean, flags);
        dest.writeString(sortName);
        dest.writeString(damageComment);
        dest.writeString(sortModel);
        dest.writeString(dateStr12);
        if (damageNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(damageNum);
        }
        if (lossesNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(lossesNum);
        }
        if (lendNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(lendNum);
        }
        if (excNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(excNum);
        }
        if (excLossesNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(excLossesNum);
        }
        if (excLocatId == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(excLocatId);
        }
        if (recordNum == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeInt(recordNum);
        }
    }
}
